package com.well.register.console.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.well.register.console.domain.Job;
import com.well.register.console.domain.JobExecutor;
import com.well.register.console.domain.JobParam;
import com.well.register.console.model.job.QueryJobExecutorParam;
import com.well.register.console.model.job.QueryJobsParam;
import com.well.register.console.model.job.UpdateJobParam;
import com.well.register.console.service.JobExecutorService;
import com.well.register.console.service.JobParamService;
import com.well.register.console.service.JobService;
import com.well.register.console.util.RpcPageResponse;
import com.well.register.console.util.RpcResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * Job manager
 * </p>
 *
 * @author weedo
 * @since 2022/8/19
 */
@RequestMapping("well-job")
@RestController
public class JobController {

    @Resource
    JobService jobService;

    @Resource
    JobExecutorService jobExecutorService;

    @Resource
    JobParamService jobParamService;


    /**
     *  查询任务列表
     * @param param
     * @return
     */
    @PostMapping("list")
    public List<Job> selectJobLists(QueryJobsParam param) {
        return jobService.selectJobLists(param);
    }

    /**
     *  停止任务
     * @param id
     * @return
     */
    @PostMapping("stop")
    public RpcResponse<Object> stopJob(Integer id) {
        jobService.stopJobById(id);
        return RpcResponse.ok();
    }

    /**
     *  重启任务
     * @param id
     * @return
     */
    @PostMapping("restart")
    public RpcResponse<Object> restartJob(Integer id) {
        jobService.restartJobById(id);
        return RpcResponse.ok();
    }

    /**
     *  手动执行任务
     * @param id
     * @return
     */
    @PostMapping("runJobNow")
    public RpcResponse<Object> runJobNow(Integer id) {
        jobService.startJobNow(id);
        return RpcResponse.ok();
    }

    /**
     * 查询任务执行记录
     * @param param
     * @return
     */
    @PostMapping("records")
    public RpcResponse<Object> executorRecord(QueryJobExecutorParam param) {
        Page<JobExecutor> executorPage = jobExecutorService.getExecutorRecordsByJobId(param);
        return RpcPageResponse.ok(executorPage.getRecords(), executorPage.getCurrent(), executorPage.getSize(), executorPage.getTotal());
    }

    /**
     * 保存任务执行参数
     * @param param
     * @return
     */
    @PostMapping("saveJobParam")
    public RpcResponse<Object> saveJobParam(JobParam param) {
        jobParamService.saveJobParam(param);
        return RpcResponse.ok();
    }

    /**
     * 根据jobId查询参数
     * @param jobId
     * @return
     */
    @PostMapping("params")
    public List<JobParam> getJobParams(Integer jobId) {
        return jobParamService.getJobParams(jobId);
    }

    /**
     * 删除jobParam
     * @param jobParamId
     * @return
     */
    @PostMapping("deleteJobParam")
    public RpcResponse<Object> deleteJobParam(Integer jobParamId) {
        jobParamService.deleteJobParam(jobParamId);
        return RpcResponse.ok();
    }

    /**
     * 更新jobParam
     * @param param
     * @return
     */
    @PostMapping("updateJobParam")
    public RpcResponse<Object> updateJobParam(UpdateJobParam param) {
        jobParamService.updateJobParam(param);
        return RpcResponse.ok();
    }
}
